﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Configuration;

public partial class Register : System.Web.UI.Page
{
    security baomat = new security();
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        try
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var checkacc = from o in dc.Users where o.Users_Name == txtUsername.Text || o.Users_Email==txtemail.Text select o;
                if (checkacc.Count() == 0)
                {
                    //User U = new User();
                    //Random r = new Random();
                    //U.Users_Id = r.Next(999);
                    //U.Users_Name = txtUsername.Text;
                    //U.Users_Password = baomat.EncryptPWD(txtpassword.Text);
                    //U.Users_Email = txtemail.Text;
                    //U.Users_FullName = txtFullname.Text;
                    //U.Users_IsEnable = 0;
                    //dc.Users.InsertOnSubmit(U);
                    //dc.SubmitChanges();
                    long? user_id=null;
                    int insertUser = dc.USP_Users_Register(ref user_id, txtUsername.Text, baomat.EncryptPWD(txtpassword.Text), txtFullname.Text, txtemail.Text, UsersType.Member, UsersIsEnable.Disable, PointBackageType.DefaultPoint);
                    if (insertUser > 0)
                    {
                        bool sendmail = baomat.sendmail(txtemail.Text, ConfigurationManager.AppSettings["Support"].ToString(), ReadTemplateActiveAccount(txtUsername.Text), "Kích hoạt tài khoản");
                        if (sendmail)
                            lblmsg.Text = "Bạn vui lòng kiểm tra email đăng kí để kích hoạt tài khoản.";
                        else
                            Response.RedirectToRoute("Error");
                    }
                    else
                    {
                        if(insertUser==-2)
                            lblmsg.Text = "Tên đăng nhập đã tồn tại";
                    }
                }
                else
                    lblmsg.Text = "Tên đăng nhập hoặc email đã tồn tại";
            }
        }
        catch (Exception ex)
        {
        }
    }
    private string ReadTemplateActiveAccount(string username)
    {
        string content = String.Empty;
        try
        {
            StringBuilder sb = new StringBuilder();
            content=baomat.ReadTemplate(Server.MapPath("template/ActiveAccount.htm"));
            content = content.Replace("{0}", username);
            content = content.Replace("{1}", ConfigurationManager.AppSettings["sitelink"].ToString() +"/"+ username + "/KichhoatTaikhoan.html");
        }
        catch
        {
        }
        return content;
    }
}